package com.bookhouse.controller;

import com.bookhouse.entity.Book;
import com.bookhouse.entity.Category;
import com.bookhouse.entity.Press;
import com.bookhouse.entity.Reader;
import com.bookhouse.service.BookService;
import com.bookhouse.service.CategoryService;
import com.bookhouse.service.PressService;
import com.bookhouse.service.ReaderService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Set;

/**
 * Created by CiusCheung on 16/4/6.
 */

@Controller
@RequestMapping("/book")
public class BookController {

    @Resource(name = "bookService")
    private BookService bookService;
    @Resource(name = "pressService")
    private PressService pressService;
    @Resource(name = "categoryService")
    private CategoryService categoryService;
    @Resource(name = "readerService")
    private ReaderService readerService;

    @RequestMapping(value = "toAddBook")
    public String toAddBook(HttpServletRequest request){
        request.setAttribute("categoryList",categoryService.findAllList(Category.class));
        request.setAttribute("pressList",pressService.findAllList(Press.class));
        return "book";
    }

    @RequestMapping(value = "addBook",method = RequestMethod.POST)
    public String addBook(Book book){
//        request.setAttribute("pressList",pressService.findAllList(Press.class));
//        request.setAttribute("categoryList",categoryService.findAllList(Category.class));
        if(book.getId() != null && book.getId()>0)
            bookService.update(book);
        else
            bookService.save(book);
        return "redirect:getAllBook";
    }

    @RequestMapping(value = "getAllBook")
    public String getAllBook(HttpServletRequest request){
        request.setAttribute("bookList",bookService.findAllList(Book.class));
        return "bookEditList";
    }

    @RequestMapping(value = "getBook")
    public String getBook(String bookId,HttpServletRequest request){
        Book book = bookService.get(Book.class,Integer.parseInt(bookId));
        request.setAttribute("pressList",pressService.findAllList(Press.class));
        request.setAttribute("book",book);
        request.setAttribute("categoryList",categoryService.findAllList(Category.class));
        return "book";
    }

    @RequestMapping(value = "getBookBorrow")
    public String getBookBorrow(String bookId,String readerId,HttpServletRequest request){
        request.setAttribute("book",bookService.get(Book.class,Integer.parseInt(bookId)));
        request.setAttribute("reader",readerService.get(Reader.class,Integer.parseInt(readerId)));
        return "borrow";
    }

    @RequestMapping(value = "deleteBook")
    public String deleteBook(String bookId){
        bookService.delete(Book.class,Integer.parseInt(bookId));
        return "redirect:getAllBook";
    }

}
